<template>
  <el-container>
    <el-header style="height: 50px">
      <span>个人信息</span>
    </el-header>
    <el-main>
      <el-form
        label-position="right"
        label-width="80px"
        ref="userInfoForm"
        :rules="userInforules"
        :model="userInfoForm"
      >
        <el-form-item
          label="用户名:"
          prop="name"
        >
          <el-input
            v-model="userInfoForm.username"
            size="small"
            placeholder="你的用户名"
            clearable
            @keyup.enter.native='centerDialogVisible = true'
          ></el-input>
        </el-form-item>
        <el-form-item
          label="邮箱:"
          prop="email"
        >
          <el-input
            v-model="userInfoForm.email"
            size="small"
            :readonly="true"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer style="margin-left:auto;margin-right:auto;">
      <el-button
        type="primary"
        round
        @click="centerDialogVisible = true"
      >保存</el-button>
      <el-dialog
        title="身份验证"
        :visible.sync="centerDialogVisible"
        width="30%"
        center
      >
        <span style="display: flex;justify-content:space-around;">
          <el-input
            type="password"
            v-model="userInfoForm.password"
            placeholder="请输入密码"
            size="small"
            :show-password="true"
            style="width:250px;"
            @keyup.enter.native="submitForm('userInfoForm')"
          ></el-input>
          <el-button
            type="primary"
            size="small"
            round
            @click="submitForm('userInfoForm')"
          >确认</el-button>
        </span>
      </el-dialog>
    </el-footer>
  </el-container>
</template>

<script>
import { request } from 'network/request'
export default {
  name: 'PersonalSetting',
  data () {
    return {
      centerDialogVisible: false,
      userInfoForm: {
        username: null,
        email: null,
        password: null,
        newPassword: null,
        checkNewPassword: null
      },
      userInforules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },
  created: function () {
    request({
      url: '/user',
      method: 'get'
    }).then((res) => {
      // console.log(res.data.user)
      this.userInfoForm.username = res.data.user.username
      this.userInfoForm.email = res.data.user.email
    })
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            url: '/user',
            method: 'put',
            data: this.userInfoForm
          }).then(res => {
            // console.log(res.data);
            // console.log(res.status);
            this.$message({
              message: '用户名更改成功',
              type: 'success',
              duration: 1000
            })
            sessionStorage.setItem('username', this.userInfoForm.username)
          }).catch(err => {
            if (err) {
              this.$message({
                message: '用户名更改失败',
                type: 'error',
                duration: 1000
              })
            }
          })
        }
        this.centerDialogVisible = false
      })
    },
    resetForm () {
      this.$refs.userInfoForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  width: 100%;
  line-height: 50px;
  display: flex;
  justify-content: flex-start;
  font-size: 16px;
  color: #00a1d6;
  border-bottom: 1px solid #e1e2e5;
}
.el-form {
  float: left;
}
</style>
